const { faceUserList, increment, faceSearch,faceMultiSearch,faceSceneGroupList,faceSceneAdd,faceFaceverify } = require('../face-api/baiduFace')

class BaiduController {
  // 组列表查询
  async getGroupList(ctx, next) { 
    const result = await faceSceneGroupList(ctx.req.token,ctx.request.body)
    ctx.body = result
  }
  // 获取用户列表
  async getUsers(ctx,next) { 
    const groupId = 'group1'
    const result = await faceUserList(ctx.req.token,groupId,ctx.request.body)
    ctx.body = result
  }
  // 人脸注册
  async add(ctx, next) {
    const { image, imageType, groupId } = ctx.request.img
    let userId = 'user1'
    const userList = await faceUserList(ctx.req.token, groupId)

    if (userList.length !== 0) {
      const num = userList[userList.length - 1].replace('user', '')
      userId = `user${parseInt(num) + 1}`
    }
    const result = await increment(
      ctx.req.token,
      image,
      imageType,
      groupId,
      userId
    )
    ctx.body = result
  }
  // 人脸搜索 1:N
  async search(ctx, next) {
    const { image, imageType, groupId } = ctx.request.img
    const result = await faceSearch(ctx.req.token, image, imageType, groupId)
    ctx.body = result
  }
  // 人脸搜索 M:N
  async multiSearch(ctx, next) {
    const { image, imageType, groupId } = ctx.request.img
    const result = await faceMultiSearch(
      ctx.req.token,
      image,
      imageType,
      groupId,
      ctx.request.body
    )
    ctx.body = result
  }
  // 场景化-人脸注册
  async getSceneAdd(ctx, next) {
    const { image, imageType } = ctx.request.img
    const groupId = 'group2'
    let userId = 'user1'
    const userList = await faceUserList(ctx.req.token, groupId)

    if (userList.length !== 0) {
      const num = userList[userList.length - 1].replace('user', '')
      userId = `user${parseInt(num) + 1}`
    }
    const result = await faceSceneAdd(
      ctx.req.token,
      image,
      imageType,
      groupId,
      userId,
      ctx.request.body
    )
    ctx.body = result
  }
  // 场景化-人脸搜索
  async getSceneSearch(ctx,next) { 
    const { image, imageType } = ctx.request.img
    const groupId = 'group2'
    const result = await faceSearch(ctx.req.token, image, imageType, groupId)
    ctx.body = result
  }
  // 在线图片活体检测
  async getFaceverify(ctx, next) {
    const { image, images, imageType } = ctx.request.img
    const newImage = images ? images : image
    const result = await faceFaceverify(ctx.req.token, newImage, imageType)
    ctx.body = result
  }
}

module.exports = new BaiduController()
